﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TugasAkhir.Helpers
{
    public class KoefisienReliabilitas
    {
        private int jumlahPertanyaan;
        private int jumlahPopulasi;
        private int[,] data;

        public KoefisienReliabilitas(int jumlahPopulasi, int jumlahPertanyaan, int[,] data)
        {
            this.jumlahPopulasi = jumlahPopulasi;
            this.jumlahPertanyaan = jumlahPertanyaan;

            this.data = data;
        }

        public double HitungKoefisienReliabilitas(List<int> pengecualian)
        {
            var Jumlah = GetJumlah(pengecualian);
            var JumlahKuadrat = GetJumlahKuadrat(pengecualian);
            var Total = GetTotal(pengecualian);
            var TotalKuadrat = GetTotalKuadrat(pengecualian);

            double sigmaVariansButir = 0.0;

            for (int i = 0; i < this.jumlahPertanyaan; i++)
            {
                //if (pengecualian.Contains(i)) continue;
                double temp = ((double)JumlahKuadrat[i] - (Math.Pow((double)Jumlah[i], 2.0) / (double)this.jumlahPopulasi)) / (double)this.jumlahPopulasi;
                sigmaVariansButir += temp;
            }

            int sumTotal = 0;
            int sumTotalKuadrat = 0;

            for (int i = 0; i < this.jumlahPopulasi; i++)
            {
                //if (pengecualian.Contains(i)) continue;
                sumTotal += Total[i];
                sumTotalKuadrat += TotalKuadrat[i];
            }

            double varians = ((double)sumTotalKuadrat - (Math.Pow((double)sumTotal, 2.0) / ((double)this.jumlahPopulasi))) / (double)this.jumlahPopulasi;

            double result = ((double)(this.jumlahPertanyaan - pengecualian.Count) / (double)(this.jumlahPertanyaan - pengecualian.Count - 1)) * ((1.0 - (sigmaVariansButir / varians)));

            return result;
        }

        private List<int> GetJumlah(List<int> pengecualian)
        {
            var result = new List<int>();

            for (int i = 0; i < this.jumlahPertanyaan; i++)
            {
                int temp = 0;
                if (!pengecualian.Contains(i))
                {
                    for (int j = 0; j < this.jumlahPopulasi; j++)
                    {
                        temp += this.data[j, i];
                    } 
                }

                result.Add(temp);
            }

            return result;
        }

        private List<int> GetJumlahKuadrat(List<int> pengecualian)
        {
            var result = new List<int>();

            for (int i = 0; i < this.jumlahPertanyaan; i++)
            {
                int temp = 0;
                if (!pengecualian.Contains(i))
                {
                    for (int j = 0; j < this.jumlahPopulasi; j++)
                    {
                        temp += (int)Math.Pow((double)data[j, i], 2.0);
                    } 
                }

                result.Add(temp);
            }

            return result;
        }

        private List<int> GetTotal(List<int> pengecualian)
        {
            var result = new List<int>();

            for (int i = 0; i < this.jumlahPopulasi; i++)
            {
                int temp = 0;
                for (int j = 0; j < this.jumlahPertanyaan; j++)
                {
                    if (!pengecualian.Contains(j))
                    {
                        temp += this.data[i, j]; 
                    }
                }

                result.Add(temp);
            }

            return result;
        }

        private List<int> GetTotalKuadrat(List<int> pengecualian)
        {
            var result = new List<int>();

            for (int i = 0; i < this.jumlahPopulasi; i++)
            {
                int temp = 0;
                for (int j = 0; j < this.jumlahPertanyaan; j++)
                {
                    if (!pengecualian.Contains(j))
                    {
                        temp += data[i, j]; 
                    }
                }

                result.Add((int)Math.Pow((double)temp, 2.0));
            }

            return result;
        }
    }
}